﻿@using StackExchange.Opserver.Data.Dashboard
@using StackExchange.Opserver.Data.Elastic
@using StackExchange.Opserver.Data.HAProxy
@using StackExchange.Opserver.Data.Redis
@using StackExchange.Opserver.Data.SQL
@{
    Layout = null;
    var issues = IssueProvider.GetIssues().ToList();
}
@functions
{
    public string GetLink(Issue i)
    {
        var node = i as Issue<Node>;
        if (node != null && node.Item != null)
        {
            return string.Format("/dashboard/node?node={0}", node.Item.PrettyName.UrlEncode());
        }
        var agIssue = i as Issue<SQLNode.AvailabilityGroupInfo>;
        if (agIssue != null && agIssue.Item != null)
        {
            return string.Format("/sql/servers#/cluster/{0}/{1}", agIssue.Item.Node.Cluster.Name.UrlEncode(), agIssue.Item.Name.UrlEncode());
        }
        var sqlInstance = i as Issue<SQLInstance>;
        if (sqlInstance != null && sqlInstance.Item != null)
        {
            return string.Format("/sql/instance?node={0}", sqlInstance.Item.Name.UrlEncode());
        }
        var redisInstance = i as Issue<RedisInstance>;
        if (redisInstance != null && redisInstance.Item != null)
        {
            return string.Format("/redis/instance?node={0}:{1}", redisInstance.Item.Host, redisInstance.Item.Port);
        }
        var elasticCluster = i as Issue<ElasticCluster>;
        if (elasticCluster != null && elasticCluster.Item != null)
        {
            return string.Format("/elastic/cluster?cluster={0}", elasticCluster.Item.Name);
        }
        var haproxyGroup = i as Issue<HAProxyGroup>;
        if (haproxyGroup != null && haproxyGroup.Item != null)
        {
            return string.Format("/haproxy?group={0}", haproxyGroup.Item.Name.UrlEncode());
        }
        return "";
    }
}
<div class="issues-list" data-issue-count="@issues.Count">
@if (issues.Any())
{
    <a href="javascript:void(0)" class="issues-button">@issues.GetWorstStatus().IconSpan() @issues.Count.Pluralize("Issue") <span class="down-arrow">▼</span></a>
    <div class="issues-dropdown">
        @foreach (var i in issues)
        {
            <a class="issue-item" href="@GetLink(i)">
                <span class="@i.MonitorStatus.Class()">@i.IconSpan() @i.Title</span>
                @if (i.Description.HasValue())
                {
                    <div class="note" title="@i.Description">@i.Description.Truncate(100)</div>
                }
            </a>
        }
    </div>
}
</div>